{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
define variable v_maillist as character no-undo.
define variable mailtitle as character.
define variable tocodepage as character.
define variable sendmail as character.
define variable mailattachment as character.
define variable reccnt as int.

reccnt = 0.


sendmail = "/home/mfg/sendmailtosales.sh".
mailattachment = "/home/mfg/Pendinginovicelist.csv".

find first code_mstr where code_domain = global_domain
                       and code_fldname = "xx_email_config"
                       and code_value = "codepage"
                       no-lock no-error.
     assign tocodepage = code_cmmt.



find first code_mstr where code_domain = global_domain
                      and code_fldname = "xx_email_config"
                      and code_value = "salestitle "
                      no-lock no-error.


    assign mailtitle = code_cmmt.

for each code_mstr where code_domain = global_domain
                 and code_fldname = "xx_email_sales"
                 no-lock.

   v_maillist = if v_maillist = "" then  code_value
                else v_maillist + ","  +  code_value .
                 
end.



output to value(mailattachment)  convert target "CP936".   


    put unformatted getTermLabel("customer",4) "," 
                    getTermLabel("customer",4) + getTermLabel("DESCRIPTION",9) ","
                  getTermLabel("BANNER_SALES_ORDER",8) ","
                  getTermLabel("ORDER_DATE",8) ","
                  getTermLabel("ORDER_LINE",8) ","
                  getTermLabel("ITEM_NUMBER",8) ","
                  getTermLabel("ITEM_NUMBER",4) + getTermLabel("DESCRIPTION",9) ","
                    getTermLabel("LOcation",8) ","
                    getTermLabel("UNINVOICED_AMOUNT",8) ","
                    getTermLabel("price",8) ","
                    getTermLabel("AMOUNT_TOTAL",8) ","
                    
                    skip. 

for each so_mstr no-lock where so_domain = global_domain
                           and so_to_inv = yes
                           and so_compl_stat = "",
        each sod_det no-lock where sod_domain = global_domain
                               and sod_nbr = so_nbr
                               and sod_qty_inv <> 0
                               and sod_compl_stat = ""
                               break by so_nbr by sod_line.
    reccnt = reccnt + 1.
    
    find pt_mstr where pt_domain = global_domain 
                   and pt_part = sod_part
                   no-lock no-error.
                   
    find ad_mstr where ad_domain = global_domain
                   and ad_addr = so_cust
                   no-lock no-error.                   

    put unformatted so_cust "," 
                    
                    ad_name     "," 
                    so_nbr      "," 
                    so_ord_date "," 
                    sod_line    "," 
                    sod_part    "," 
                    pt_desc1    ","
                    sod_loc     "," 
                    sod_qty_inv "," 
                    sod_price   ","
                    sod_qty_inv * sod_price skip.                    
end.

output close.

if reccnt > 0 then 
do:

output to value(sendmail)  convert target tocodepage.

put unformatted "/home/mfg/App-swaks-20190914.0/swaks --body " + quoter( mailtitle) +
                " --attach " + mailattachment +
                " --tls --from qad@faway.com --header " + quoter( "Subject:" + mailtitle) + 
                " --server smtp.partner.outlook.cn --port 587 " + 
                "--auth-user qad@faway.com --auth-password Faway@2024 --to ".

put unformatted v_maillist  skip.


output close.


    unix silent value("chmod 766 " + sendmail).
    unix silent value(sendmail).
    unix silent value("mv " + sendmail + " " + sendmail + ".bak").
    unix silent value("mv " + mailattachment + " " + mailattachment + ".bak").
    
end.    